//---------------------------------------------------------------------------
// CSignalMask Class
// Author : Woo-Hyun Kim
// Email : woorung@nhncorp.com
// Date : Apr. 7, 2008
//---------------------------------------------------------------------------
#ifndef CSignalMaskH
#define CSignalMaskH

#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h> 
#include <errno.h>

#include <pthread.h>

using namespace std;
//---------------------------------------------------------------------------
class CSignalMask {
private :
	sigset_t sigset;
	sigset_t oldset;
	siginfo_t siginfo;

public :
        CSignalMask() {
		sigemptyset(&sigset);
	};
        ~CSignalMask() {};

	void add(int signum) {
		sigaddset(&sigset, signum);
	};

	void del(int signum) {
		sigdelset(&sigset, signum);
	};

	void block() {
		sigprocmask(SIG_BLOCK, &sigset, &oldset);
		//pthread_sigmask(SIG_BLOCK, &sigset, &oldset);
	};

	void unblock() {
		sigprocmask(SIG_UNBLOCK, &sigset, &oldset);
		//pthread_sigmask(SIG_UNBLOCK, &sigset, &oldset);
	};

	void setmask() {
		sigprocmask(SIG_SETMASK, &sigset, &oldset);
		//pthread_sigmask(SIG_SETMASK, &sigset, &oldset);
	};

	// setinit() must be called after setmask()
	void setinit() {
		sigprocmask(SIG_SETMASK, &oldset, NULL);
		//pthread_sigmask(SIG_SETMASK, &oldset, NULL);
	};

	// waitinfo() must be called after block() or setmask() 
	int waitinfo() {
		return sigwaitinfo(&sigset, &siginfo);
	};

	// waitinfo(time) must be called after block() or setmask() 
	int timedwaitinfo(const struct timespec *timeout) {
		return sigtimedwait(&sigset, &siginfo, timeout);
	};

	int get_wait_pid() {
		return siginfo.si_pid;
	};

	int get_wait_value_int() {
		return siginfo.si_value.sival_int;
	};

	void *get_wait_value_ptr() {
		return siginfo.si_value.sival_ptr;
	};
};
//---------------------------------------------------------------------------
#endif
